<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="搜索内容" prop="searchContent">
        <el-input
          v-model="queryParams.searchContent"
          placeholder="请输入场站名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场站ID" prop="stationId">
        <el-select
          v-model="queryParams.stationId"
          placeholder="场站ID"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in this.stationList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleCopyAdd"
        >复制新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatchDelete"
        >批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="terminalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <!-- 自增序号列 -->
      <el-table-column type="index" label="序号">
        <!-- 自定义索引 (可选) -->
        <template slot-scope="scope">
          {{ serial_number + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="协议ID" align="center" key="protocolId" prop="protocolId" v-if="columns[0].visible" width="80"/>
      <el-table-column label="服务器IP" align="center" key="serverIp" prop="serverIp" v-if="columns[1].visible" width="160" />
      <el-table-column label="端口号" align="center" key="serverPort" prop="serverPort" v-if="columns[2].visible" width="80"/>
      <el-table-column label="终端标识" align="center" key="terminalId" prop="terminalId" v-if="columns[3].visible"  width="150"/>
      <el-table-column label="场站ID" align="center" key="stationId" prop="stationId" v-if="columns[4].visible" width="200" />
      <el-table-column label="场站名称" align="center" key="stationName" prop="stationName" v-if="columns[5].visible" width="220" />
      <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[7].visible" width="200" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:station:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:station:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场站配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="协议ID" prop="protocolId">
              <el-input v-model="form.protocolId" placeholder="请输入协议ID" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="服务器IP" prop="serverIp">
              <el-input v-model="form.serverIp" placeholder="请输入服务器IP" maxlength="64"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="端口号" prop="serverPort">
              <el-input v-model="form.serverPort" type="text" placeholder="请输入端口号" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="终端标识" prop="terminalId">
              <el-input v-model="form.terminalId" type="text" placeholder="请输入终端标识" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="场站ID" prop="stationId">
              <el-select
                v-model="form.stationId"
                placeholder="场站ID"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in this.stationList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="解析类" prop="classInstance">
              <el-input v-model="form.classInstance" type="text" placeholder="请输入真实解析类" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getTerminalInfoList,
  getTerminalInfoDetail,
  addTerminalInfo,
  updateTerminalInfo,
  delTerminalInfo,
  changeTerminalStatus, batchDelTerminalInfo
} from "@/api/system/terminal";
import {getStationDictList} from "@/api/system/dict";

export default {
  name: "Terminal",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      terminalList: [],
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        id: undefined,
        protocolId: '',
        serverIp: '',
        serverPort: '',
        stationId: '',
        terminalId: '',
        classInstance: ''
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchContent: '',
        stationId: ''
      },
      // 列信息
      columns: [
        {key: 0, label: `协议ID`, visible: true},
        {key: 1, label: `服务器IP`, visible: true},
        {key: 2, label: `服务器端口号`, visible: true},
        {key: 3, label: `终端标识`, visible: true},
        {key: 4, label: `场站ID`, visible: true},
        {key: 5, label: `场站名称`, visible: true},
        {key: 7, label: `创建时间`, visible: true}
      ],
      // 表单校验
      rules: {
        protocolId: [
          {required: true, message: "协议ID不能为空", trigger: "blur"}
        ],
        stationId: [
          {required: true, message: "场站编号不能为空", trigger: "blur"},
          {min: 16, max: 32, message: '场站编号长度必须介于 16 和 32 之间', trigger: 'blur'}
        ],
        protocolIp: [
          {required: true, message: "协议IP不能为空", trigger: "blur"}
        ],
        terminalId: [
          {required: true, message: "终端标识不能为空", trigger: "blur"}
        ],
        serverIp: [
          {required: true, message: "服务器IP不能为空", trigger: "blur"}
        ],
        serverPort: [
          {required: true, message: "服务器端口号不能为空", trigger: "blur"}
        ]
      }
    };
  },
  computed: {
    serial_number() {
      return this.queryParams.pageSize * (this.queryParams.pageNum - 1);
    }
  },
  created() {
    this.loading = false;
    this.getStationList();
    this.getList();
  },
  methods: {
    getStationList() {
      getStationDictList().then(response => {
        let rows = response.data;
        this.stationList = rows.map(style => ({
          value: style.stationId,
          label: style.stationName,
        }));
      });
    },
    /** 查询场站列表 */
    getList() {
      this.loading = true;
      getTerminalInfoList(this.queryParams).then(response => {
          this.terminalList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        protocolId: undefined,
        serverIp: undefined,
        serverPort: undefined,
        stationId: undefined,
        stationName: undefined,
        terminalId: undefined,
        enable: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加终端信息";
    },
    handleCopyAdd(){
      const ids = this.ids;
      if (ids.length > 1){
        this.$modal.msgWarning("只能选择一个");
        return;
      }
      const id = ids[0];
      this.disabled = false;
      this.reset();
      getTerminalInfoDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改终端信息";
        this.form.id = undefined;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getTerminalInfoDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改终端信息";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTerminalInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTerminalInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除终端信息"' + id + '"的数据项？').then(function () {
        return delTerminalInfo(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleBatchDelete() {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除终端信息"' + ids + '"的数据项？').then(function () {
        return batchDelTerminalInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }

};
</script>
